package com.dxyxx.framework;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;

import com.dxyxx.R;
import com.dxyxx.android.setting.UserPreferences;
import com.http.HttpUtil;
import com.nostra13.example.universalimageloader.ScaleBitmapDisplayer;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class BaseFragment extends Fragment {
	protected String baseImageURL = HttpUtil.baseImageURL;
	protected int COUNT = 5;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	private static ObjectMapper objectMapper = new ObjectMapper();

	public static ObjectMapper getObjectMapper() {
		if (objectMapper != null) {
			return objectMapper;
		} else {
			return new ObjectMapper();
		}
	}

	protected ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	// 长方形640*240。。homepage bannner
	//protected DisplayImageOptions rectangularOptions = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_long).showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).cacheInMemory(true).cacheOnDisc(true).considerExifParams(true).displayer(new ScaleBitmapDisplayer()).build();
	//正方形
	protected DisplayImageOptions squareOptions = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_stub).showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).cacheInMemory(true).cacheOnDisc(true).considerExifParams(true).displayer(new ScaleBitmapDisplayer()).build();

	public static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
	
	

}
